package com.example;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

public class RedisMain {

    private static JedisCluster jedisCluster;


    public static void main(String[] args) {
        if (args.length <= 0 ){
            System.out.println("please input set/get param,example \"set city shanghai\" or \"get city\"");
        }
        // 建立连接池
        setJedisCluster();
        // set 数据
        addData(args[1],args[2]);
        // get 数据
        getData(args[1]);
    }

    private static void setJedisCluster(){
        Set<HostAndPort> hostAndPorts = new HashSet<>();
        hostAndPorts.add(new HostAndPort("106.12.89.154",7001));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7002));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7003));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7004));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7005));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7006));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7007));
        hostAndPorts.add(new HostAndPort("106.12.89.154",7008));
        jedisCluster = new JedisCluster(hostAndPorts);
    }

    public static void addData(String key,String value){
        System.out.println("add key="+key+",value="+value+",result="+jedisCluster.set(key, value));
    }

    public static void getData(String key){
        System.out.println("get key="+key+",result="+jedisCluster.get(key));
    }
}
